<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Services_Page
{
	private static $_blockNameNotIn = array(
						'APP_STATIC_SERVER', 'APP_RESOURCE_SERVER', 'APP_WEB_URL',
						'APP_WEB_NAME', 'APP_TEMPLATE', 'APP_SKIN', 'APP_META_KEYWORD', 
						'APP_META_DESCRIPTION', 'APP_LANG', 'STRING', 'GA_CODE',
						'TRANSLATOR', 'MESSENGER', 'JSON', 'CONFIG',
					);
	public static function validate($data)
	{
		$validator = new Gio_Core_Validator();
		foreach ($data as $index => $value) {
			if (gettype($value) == 'string') {
				$data[$index] = trim($value);
			}
		} 
												
		$data['messages']['name'] = $validator->setString($data['name'])
												->validate(array('notNull'), array('notNull' => 'page_name_notnull'));
		$data['messages']['description'] = $validator->setString($data['description'])
												->validate(array('notNull'), array('notNull' => 'page_description_notnull'));
		$data['messages']['route'] = $validator->setString($data['route'])
												->validate(array('notNull'), array('notNull' => 'page_route_notnull'));

		$error = false;
		foreach ($data['messages'] as $index => $message) {
			if ($message) {
				$data['messages_error'] = true;
				return $data;
			}
		}										
		$data['messages_error'] = false;
		return $data; 
	}
	
	public static function add($page)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$pageDao = new Modules_Core_Models_Mysql_Page();
		$pageDao->setConnection($conn);
		/**
		 * Create route layout
		 */
		$configs = Gio_Core_Config_Xml::getConfig('web');
		$filename = $page['route'] . '.phtml';
		$file = TEMPLATE_DIR . DS . $configs->template . DS . 'layouts' . DS . $filename;
		$content = null;
		if (file_exists($file)) {
			$content = file_get_contents($file);
		}
		Gio_Core_File::writeToFile($file, $content);
		
		return $pageDao->add($page);
	}
	
	public static function delete($pageId)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$pageDao = new Modules_Core_Models_Mysql_Page();
		$pageDao->setConnection($conn);
		return $pageDao->delete($pageId);
	}
	
	public static function update($page)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$pageDao = new Modules_Core_Models_Mysql_Page();
		$pageDao->setConnection($conn);
		return $pageDao->update($page);
	}
	
	public static function find($condition = array(), $start, $perPage)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$pageDao = new Modules_Core_Models_Mysql_Page();
		$pageDao->setConnection($conn);
		return $pageDao->find($condition, $start, $perPage);
	}
	
	public static function count($condition = array())
	{
		$conn		= Gio_Db_Connection::getConnection();
		$pageDao = new Modules_Core_Models_Mysql_Page();
		$pageDao->setConnection($conn);
		return $pageDao->count($condition);
	}
	
	public static function getById($id)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$pageDao = new Modules_Core_Models_Mysql_Page();
		$pageDao->setConnection($conn);
		return $pageDao->getById($id);
	}
	
	public static function dbPages($fieldsName = null)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$pageDao = new Modules_Core_Models_Mysql_Page();
		$pageDao->setConnection($conn);
		return $pageDao->dbPages($fieldsName);
	}
	
	public static function import($layoutFile)
	{
		$content = file_get_contents($layoutFile);
		$pattern = '/(\<\?php echo \$this-\>)([a-zA-Z0-9_]+)(\;[\W]\?\>)/i';
		$blocks = array();
		if (preg_match_all($pattern, $content, $matches)) {
			$blocks = $matches[2];
		}
		$ret = array();
		if ($blocks) {
			foreach ($blocks as $index => $block) {
				if (!in_array($block, self::$_blockNameNotIn)) {
					$ret[] = $block;
				}
			}
		}
		return $ret;
	}
	
	public static function importXml($dataFile)
	{
		$xml = @simplexml_load_file($dataFile);
		$blocks = isset($xml->block) ? $xml->block : null;
		$ret = array();
		if ($blocks) {
			$widgetIndex = 0;
			foreach ($blocks as $index => $block) {
				$widgetIndex++;
				$data = array();
				$data['index'] = $widgetIndex;
				$data['blockname'] = (string)$block->block_name;
				$data['type'] = (string)$block->type;
				$data['module'] = (string)$block->module;
				$data['name'] = (string)$block->name;
				$data['action'] = (string)$block->action;
				
				$data['load'] = (isset($block->load) && (string)$block->load == 'ajax')
									? 'ajax' : 'php';
				$data['cache']['enable'] = (isset($block->cache->enable) && (string)$block->cache->enable == 'true')
												? true : false;
				$data['visible'] = (isset($block->visible) && (string)$block->visible == 'false')
												? false : true;
				$data['cache']['timeout'] = (isset($block->cache->timeout) && $block->cache->timeout > 0)
												? (int)$block->cache->timeout : 0;
				$data['params'] = (isset($block->params))
										? (array)$block->params : null;
				$paramString = 'widget_index=' . $data['index'];
				if ($data['params']) {
					foreach ($data['params'] as $key => $value) {
						$paramString .= (null == $paramString) ? '' : '|';
						$paramString .= $key . '=' . $value;
					}
				}
				$data['paramString'] = $paramString;
				$ret[$data['blockname']] = $data;
			}
		}
		return $ret;
	}
}